package com.boco.modules.fdoc.api;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.boco.common.constants.BusinessConstants;
import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.json.MessageResult;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.model.ResidentEntity;
import com.boco.modules.fdoc.service.ResidentService;
import com.boco.modules.fdoc.vo.ResidentVo;
import com.wordnik.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value="/resident",produces="text/json;charset=utf-8")
public class ResidentApi {
	
	@Resource
	ResidentService residentService;
	
	@RequestMapping(value = "/addRsident", method = RequestMethod.POST)
	@ApiOperation(value = "添加居民", notes = "{'personName':'居民姓名','sex':'性别0:男  1:女','timeStamp':'出生日期时间戳','idCard':'身份证号','phoneNumber':'电话号码',"
			+ "'workunits':'工作单位','householdaddress':'户籍所在地','permanent':' 0：常住户口所在  1：不常住户口所在地','nowaddress':'现居住地址','masterIdCard':'户主身份证号',"
			+ "'parentHouseRelation':'关系：0:本人，1.父母，2：妻子   3：子女  4：其它','createBy':'添加人','remarks':'备注'}", response = MessageResult.class)
	public String addMember(@RequestBody String memberBody) {
		ResidentVo vo = JsonUtils.getObject(memberBody, ResidentVo.class);
		ResidentEntity en = new ResidentEntity();
		en.setPersonName(vo.getPersonName());
		en.setSex(vo.getSex());
		en.setDateOfBirth(new Date(vo.getTimeStamp()));
		en.setIdCard(vo.getIdCard());
		en.setPhoneNumber(vo.getPhoneNumber());
		en.setWorkunits(vo.getWorkunits());
		if (StringUtils.isNotEmpty(vo.getHouseholdaddress())) {
			en.setHouseholdaddress(vo.getHouseholdaddress());
		}
		en.setPermanent(vo.getPermanent());
		en.setNowaddress(vo.getNowaddress());
		en.setMasterIdCard(vo.getMasterIdCard());
		en.setParentHouseRelation(vo.getParentHouseRelation());
		en.setCreateBy(vo.getCreateBy());
		en.setRemarks(vo.getRemarks());
		en.setStatus(BusinessConstants.RESIDENG_STATUS_SELFUPDATE);  //设置为手动修改
		int returnInt = residentService.addResident(en);
		if (returnInt == -1) {
			return JsonUtils.getJson(BaseJsonVo.empty(ApiStatusEnum.RE_ADD_RESIDENT.getKey(), ApiStatusEnum.RE_ADD_RESIDENT.getValue()));
		}
		return JsonUtils.getJson(BaseJsonVo.success(returnInt));
	}
	
	@RequestMapping(value = "/getResidenceMaster", method = RequestMethod.GET)
	@ApiOperation(value = "根据居民身份证号获取户主信息", notes = "{'idCard':'居民身份证号'}", response = MessageResult.class)
	public String getResidenceMaster(@RequestParam String idCard) {
		return JsonUtils.getJson(BaseJsonVo.success(residentService.getResidentMaster(idCard)));
	}
	
	@RequestMapping(value = "/getResidenceMember", method = RequestMethod.GET)
	@ApiOperation(value = "根据居民身份证号获取户口中所有成员信息", notes = "{'idCard':'居民身份证号'}", response = MessageResult.class)
	public String getResidenceMember(@RequestParam String idCard) {
		return JsonUtils.getJson(BaseJsonVo.success(residentService.getResidenceMember(idCard)));
	}
	
	@RequestMapping(value = "/getAllResident", method = RequestMethod.GET)
	@ApiOperation(value = "通过身份证查看当前家庭所有成员及签约情况",notes = "{'idCard':'签约人身份证'}")
	public String getAllResident(@RequestParam String idCard) {
		return JsonUtils.getJson(BaseJsonVo.success(residentService.getAllResidentSignSituation(idCard)));
	}
	
	@RequestMapping(value = "/getResidentDetail", method = RequestMethod.GET)
	@ApiOperation(value = "通过身份证家庭成员签约详情",notes = "{'idCard':'签约人身份证'}")
	public String getResidentDetail(@RequestParam String idCard) {
		return JsonUtils.getJson(BaseJsonVo.success(residentService.getResidentDetail(idCard)));
	}
	
	@RequestMapping(value = "/deleteResident", method = RequestMethod.POST)
	@ApiOperation(value = "删除居民",notes = "{'id':'居民ID'}")
	public String deleteResident(@RequestBody String memberBody) {
		ResidentVo vo = JsonUtils.getObject(memberBody, ResidentVo.class);
		return JsonUtils.getJson(BaseJsonVo.success(residentService.delete(vo.getId())));
	}
	
	@RequestMapping(value = "/updateRsident", method = RequestMethod.POST)
	@ApiOperation(value = "修改居民信息", notes = "{'id':'居民id','personName':'居民姓名','sex':'性别0:男  1:女','timeStamp':'出生日期时间戳','phoneNumber':'电话号码'}", response = MessageResult.class)
	public String updateRsident(@RequestBody String memberBody) {
		ResidentVo paramVo = JsonUtils.getObject(memberBody, ResidentVo.class);
		ResidentEntity itemEntity = residentService.getResident(paramVo.getId());
		itemEntity.setPersonName(paramVo.getPersonName());
		itemEntity.setSex(paramVo.getSex());
		itemEntity.setDateOfBirth(new Date(paramVo.getTimeStamp()));
		itemEntity.setPhoneNumber(paramVo.getPhoneNumber());
		int returnInt = residentService.update(itemEntity);
		return JsonUtils.getJson(BaseJsonVo.success(returnInt));
	}
}
